package com.iking.toolkit.formatdeal.controller;

import com.iking.toolkit.formatdeal.model.dto.AddWatermarkToPdfDTO;
import com.iking.toolkit.formatdeal.model.dto.FileUrlDTO;
import com.iking.toolkit.formatdeal.service.WatermarkService;
import com.iking.toolkit.model.Msg;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 功能描述:水印管理
 *
 * @author: yfl
 * @date: 2025/8/4 17:49
 */
@Slf4j
@Tag(name = "WatermarkController", description = "水印管理")
@RequestMapping("/watermark")
@RestController
@Validated
public class WatermarkController {

    @Autowired
    private WatermarkService watermarkService;

    @Operation(summary = "添加暗水印到pdf文件上")
    @PostMapping(value = "/addWatermarkToPdf", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Msg addWatermarkToPdf(
            @RequestParam("file") MultipartFile multipartFile,
            // 所有参数通过表单字段传递，而非JSON
            @RequestParam(value = "watermarkText", required = true, defaultValue = "IKINGTECH") String watermarkText,
            @RequestParam(value = "angle", required = true, defaultValue = "100") Integer angle,
            @RequestParam(value = "fontSize", required = true, defaultValue = "35") Integer fontSize,
            @RequestParam(value = "dpi", required = true, defaultValue = "144") Integer dpi,
            HttpServletResponse response) {
        // 手动构建DTO对象
        AddWatermarkToPdfDTO dto = new AddWatermarkToPdfDTO();
        dto.setWatermarkText(watermarkText);
        dto.setAngle(angle);
        dto.setFontSize(fontSize);
        dto.setDpi(dpi);
        return watermarkService.addWatermarkToPdf(multipartFile, dto, response);
    }

    @Operation(summary = "添加暗水印到pdf文件url上")
    @PostMapping(value = "/addWatermarkToUrl")
    public Msg addWatermarkToUrl(@RequestBody @Valid AddWatermarkToPdfDTO addWatermarkToPdfDTO, HttpServletResponse response) {
        return watermarkService.addWatermarkToUrl(addWatermarkToPdfDTO, response);
    }

    @Operation(summary = "上传pdf文件,解析暗水印,返回暗水印图片")
    @PostMapping(value = "/extractWatermarkFromPdf", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Msg extractWatermarkFromPdf(@RequestParam("file") MultipartFile multipartFile, HttpServletResponse response) {
        return watermarkService.extractWatermarkFromPdf(multipartFile, response);
    }
    @Operation(summary = "上传图片,解析暗水印,返回暗水印图片")
    @PostMapping(value = "/extractWatermarkFromImage", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Msg extractWatermarkFromImage(@RequestParam("file") MultipartFile multipartFile, HttpServletResponse response) {
        return watermarkService.extractWatermarkFromImage(multipartFile, response);
    }
}
